package com.example.camel.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.camel.*;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.spi.ComponentCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.ConnectionFactory;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.UUID;
import java.util.stream.Collectors;

@Configuration
@Slf4j
public class CamelConfig implements ComponentCustomizer<JmsComponent> {

    private final CamelContext CAMEL_CONTEXT;

    private final ConnectionFactory connectionFactory;

    public CamelConfig(CamelContext camelContext,ConnectionFactory connectionFactory) {
        this.CAMEL_CONTEXT = camelContext;
        this.connectionFactory = connectionFactory;
    }



    @Bean
    public RoutesBuilder routesBuilder() {
        String queueName = "jms:queue:files";
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                // 1.将in的文件拷贝到out中并输出
                from("file://{{user.home}}/Desktop/in").routeId("in-to-out").to("file://{{user.home}}/Desktop/out?autoCreate=false");
                // 2. 读取文件中每一行并输出到jms队列上
                from("file://{{user.home}}/Desktop/to-jms").routeId("file-to-jms")
                        .transform()
                        .body(GenericFile.class, genericFile -> {
                            File file = File.class.cast(genericFile.getFile());
                            try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
                                return in.lines().collect(Collectors.joining());
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        })
                        .process().exchange(exchange -> {
                            Message in = exchange.getIn();
                    String body = in.getBody(String.class);
                    log.info("读取到文件内容: " + body);
                })
                         // 3. 当读取到内容保护haha则发送消息到jms:queue:haha上
                    .choice()
                        .when(exchange -> {
                    Message in = exchange.getIn();
                    String body = in.getBody(String.class);
                   return  body.contains("haha");
                }) .process().exchange(exchange -> {
                    Message in = exchange.getIn();
                    String body = in.getBody(String.class);
                    log.info("发生到hah的消息队列内容为: " + body);
                })
                        .to("jms:queue:haha")
                        // 4. 不包含haha的消息发送到jms:queue:files上
                        .otherwise()
                        .process().exchange(exchange -> {
                    Message in = exchange.getIn();
                    String body = in.getBody(String.class);
                    log.info("发送到file队列的消息为: " + body);
                })
                        .to(queueName)
                        .endChoice();

                // 5. 读取jms:queue:files队列里的消息并输出到本地文件上
                from(queueName)
                        .routeId("jms-to-file")
                        .setHeader("CamelFileName",()-> UUID.randomUUID().toString()+".txt")
                        .to("file://{{user.home}}/Desktop/from-jms");

            }
        };
    }

    @Override
    public void customize(JmsComponent jmsComponent) {
        jmsComponent.setConnectionFactory(connectionFactory);
    }
}
